/*
 * This file is part of the Boring Engine.
 *
 * Copyright (c) 2011 by pf5234 <pf5234@users.sourceforge.net>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

package com.googlecode.boringengine.internal.java2drendering;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Composite;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import com.googlecode.boringengine.Log;
import com.googlecode.boringengine.Sprite;
import com.googlecode.boringengine.internal.Renderer;
import java.awt.Rectangle;

public class Java2DRenderer extends Renderer {
    
    private BufferedImage backBuffer;
    private Graphics2D bufferGraphics;

    public Java2DRenderer() {
        backBuffer = new BufferedImage(800, 600, BufferedImage.TYPE_3BYTE_BGR);
        bufferGraphics = backBuffer.createGraphics();
    }

    @Override
    public void clearScreen() {
        if (_alpha != 255)
            bufferGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha/255f));
        bufferGraphics.setColor(Color.white);
        bufferGraphics.fillRect(0, 0, 800, 600);
    }

    @Override
    public void clip(int x, int y, int width, int height) {
        bufferGraphics.clipRect(x, y, width, height);
    }

    @Override
    public Sprite createSpriteFromScreen(int x, int y, int width, int height) {
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        image.createGraphics().drawImage(backBuffer, null, -x, -y);
        return new Java2DSprite(image);
    }

    @Override
    public Sprite createSprite(String sprite, int width, int height) {
        return new Java2DSprite(sprite, width, height);
    }

    @Override
    public void drawRect(int w, int h, int color) {
        AffineTransform af = bufferGraphics.getTransform();
        bufferGraphics.translate(_x + (_flip ? w * _scale : 0), _y);
        bufferGraphics.rotate(Math.toRadians(_rotation),
                w / 2 * (_flip ? -1 : 1) * _scale, h / 2 * _scale);
        bufferGraphics.scale((_flip ? -1 : 1) * _scale, _scale);
        Color old = bufferGraphics.getColor();
        Composite oldComp = bufferGraphics.getComposite();
        if (_alpha != 255)
            bufferGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha/255f));
        int rgb = 255 * color / 9;
        bufferGraphics.setColor(new Color(rgb, rgb, rgb));
        bufferGraphics.fillRect(0, 0, w, h);
        bufferGraphics.setColor(old);
        bufferGraphics.setComposite(oldComp);
        bufferGraphics.setTransform(af);
    }

    @Override
    public void drawSprite(Sprite sprite) {
        AffineTransform af = bufferGraphics.getTransform();
        bufferGraphics.translate(_x + (_flip ? sprite.getWidth() * _scale : 0), _y);
        bufferGraphics.rotate(Math.toRadians(_rotation),
                sprite.getWidth() / 2 * (_flip ? -1 : 1) * _scale, sprite.getHeight() / 2 * _scale);
        bufferGraphics.scale((_flip ? -1 : 1) * _scale, _scale);
        Composite oldComp = bufferGraphics.getComposite();
        if (_alpha != 255)
            bufferGraphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, _alpha/255f));
        bufferGraphics.drawImage(((Java2DSprite)sprite).getImage(), 0, 0, null);
        bufferGraphics.setComposite(oldComp);
        bufferGraphics.setTransform(af);
    }

    @Override
    public boolean takeScreenshot(String screenshotName) {
        try {
            return ImageIO.write(backBuffer, "png", new File(screenshotName));
        } catch (IOException ex) {
            Log.error("Could not write screenshot: %s", ex.getMessage());
            return false;
        }
    }

    @Override
    public void unClip() {
        bufferGraphics.setClip(null);
    }
    
    BufferedImage getImage() {
        return backBuffer;
    }

    @Override
    public void uploadSprites() {}

}
